﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolLibrary.Utils;

namespace SchoolLibrary.Desktop.Common.Component
{
    public class FixedLenghtTextBox : TextBox
    {
        public const int UNBOUNDED_LENGHT = -1;

        private int maxLenght = UNBOUNDED_LENGHT;

        public FixedLenghtTextBox()
        {
            this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.keyPress);
        }


        private void keyPress(object sender, KeyPressEventArgs e)
        {
            if (maxLenght != UNBOUNDED_LENGHT 
                && this.Text.Length >= maxLenght && e.KeyChar != StringUtil.BACKSPACE_CHAR_CODE)
            {
                e.Handled = true;
            }
        }

        public int MaxLenght
        {
            get { return maxLenght; }
            set { this.maxLenght = value; }
        }
    }
}
